package models

import (
	"gorm.io/gorm"
	"house-src/basic/config"
)

// 保洁师
type Cleaner struct {
	gorm.Model
	CustomerId  int    `json:"customer_id" gorm:"type:int;comment:'用户id'"`
	RealName    string `json:"real_name" gorm:"type:varchar(30);comment:'真实姓名'"`
	Desc        string `json:"desc" gorm:"type:varchar(200);comment:'描述'"`
	Address     string `json:"address" gorm:"type:varchar(200);comment:'地址'"`
	ClassType   string `json:"class_type" gorm:"type:varchar(30);comment:'分类'"`
	Image       string `json:"image" gorm:"type:varchar(300);comment:'图片'"`
	Status      int    `json:"status" gorm:"type:int;comment:'状态 1-空闲中 2-忙碌中'"`
	CheckStatus int    `json:"check_status" gorm:"type:int;;comment:'审核状态 1-待审核 2-审核通过 3-审核未通过'"`
}

func (cl *Cleaner) TableName() string {
	return "cleaner"
}

// 添加
func (cl *Cleaner) AddCleaner() error {
	return config.DB.Create(&cl).Error
}

// 根据id查询
func (cl *Cleaner) GetCleanerById(id int) error {
	return config.DB.Model(&Cleaner{}).Where("id = ?", id).Limit(1).Find(&cl).Error
}

// 清洁师认证
func (cl *Cleaner) AuthCleaner(id int) error {
	return config.DB.Model(&Cleaner{}).Where("id = ?", id).Updates(&cl).Error
}

// 审核状态
func (cl *Cleaner) UpdateCheckStatus(id int) error {
	return config.DB.Model(&Cleaner{}).Where("id = ?", id).Updates(&cl).Find(&cl).Error
}

// 列表
func (cl *Cleaner) GetCleanerList(classType string) (cleanerList []*Cleaner, err error) {
	db := config.DB.Model(&Cleaner{})
	if classType != "" {
		db = db.Where("class_type like ?", "%"+classType+"%")
	}
	db.Find(&cleanerList)
	return cleanerList, err
}

// 查找空闲清洁师
func (cl *Cleaner) GetFreeCleaner() (cleanerId int) {
	config.DB.Model(&Cleaner{}).Where("status = 1").Find(&cl)
	return
}

// 更新接单状态
func (cl *Cleaner) UpdateStatus(id int) error {
	return config.DB.Model(&Cleaner{}).Where("id = ?", id).Updates(&cl).Find(&cl).Error
}
